package com.github.soiff.shadow.plugin.helper;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

import java.io.IOException;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class ComponentHelper implements Helper<String> {
    @Override
    public CharSequence apply(String className, Options options) throws IOException {
        boolean isDesignMode = options.get("_DESIGN_MODE_") != null;
        className = className + " eve-component";
        Object customClassName = options.context.get("_CLASS_");
        StringBuilder compOpenTag = (new StringBuilder("<div class=\"")).append(className);
        if(customClassName != null) {
            compOpenTag.append(" ").append(customClassName);
        }

        compOpenTag.append("\"");
        Object style = options.context.get("_STYLE_");
        if(style != null) {
            compOpenTag.append(" style=\"").append(style).append("\"");
        }

        Object compName = options.context.get("_COMP_NAME_");
        if(compName != null) {
            compOpenTag.append(" data-comp-name=\"").append(compName).append("\"");
        }

        Object compData = options.context.get("_COMP_DATA_");
        if(compData != null) {
            compOpenTag.append(" data-comp-data=\"").append(compData).append("\"");
        }

        Object compPath = options.context.get("_COMP_PATH_");
        if(compPath != null) {
            compOpenTag.append(" data-comp-path=\"").append(compPath).append("\"");
        }

        if(isDesignMode) {
            compOpenTag.append(" data-comp-class=\"").append(className).append("\"");
        }

        compOpenTag.append(" >");
        return new Handlebars.SafeString(compOpenTag.toString() + options.fn() + "</div>");
    }
}
